<?php

namespace App\Admin\Controllers;

use App\Events\MoneyChangeEvent;
use App\Models\Fund;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class FundController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('基金');
            $content->description('列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('基金');
            $content->description('审核');

            $content->body($this->form($id)->edit($id));
        });
    }

//    /**
//     * Create interface.
//     *
//     * @return Content
//     */
//    public function create()
//    {
//        return Admin::content(function (Content $content) {
//
//            $content->header('header');
//            $content->description('description');
//
//            $content->body($this->form());
//        });
//    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Fund::class, function (Grid $grid) {

            $grid->id('ID')->sortable();
            $grid->order_no('订单号');
            $grid->num('单位');
            $grid->year('年限');
            $grid->user()->name('购买人');
            $grid->status('支付状态')->display(function ($status) {
                if ($status) {
                    return "<span class='label label-primary'>已支付</span>";
                } else {
                    return "<span class='label label-primary'>未支付</span>";
                }
            });
            $grid->actions(function ($actions) {
                $actions->disableDelete();
//                $actions->disableEdit();
                // append一个操作
//                $actions->append('<a href="">查看收益详情</a>');
            });
            $grid->tools(function ($tools) {
                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                });
            });

            $grid->created_at('购买时间');
//            $grid->updated_at('');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($id  = 0)
    {
        return Admin::form(Fund::class, function (Form $form) use ($id) {


            $form->display('id', 'ID');
            $form->display('order_no', '订单号');
            $form->display('year', '年限');
            $form->display('num', '单位');
//            $form->display('money','总金额');
            $form->switch('status', '支付状态');
            $form->display('created_at', '创建时间');
            $form->display('updated_at', '更新时间');
            if($id){
                $stock = Fund::find($id);
                if ($stock->status == 1) {
                    $form->disableSubmit();
                }
            }

            $form->disableReset();
            $form->saved(function ($form) {
                $fund = $form->model();
                /** @var \App\Models\Fund $fund */

                $money = $fund->num * $fund->price;
                $order_no = $fund->order_no;
                $parent = $fund->user->parent;
//                if ($parent != 0) {
                    event(new MoneyChangeEvent($order_no, 'fund', $money, $parent, $fund->user_id));
//                }
            });
        });
    }
}
